#ifndef __TERRAINOBJECT_H__
#define __TERRAINOBJECT_H__

#include <standard/reference.h>
#include <standard/raycaster.h>
#include <standard/xml.h>
#include <bigimage/renderitem.h>
#include <bigimage/scene.h>


struct STerrainSelectedPointInfo{
    float m_fPosX, m_fPosY;
    int32 m_nCellX, m_fCellY;
    int32 m_nBlockX, m_nBlockY;
    float m_fHeight;
};

class CTerrainBlock;
class CTerrainTextureList;
class CFileWriter;
class CTerrainObject: public CGameObject{
public:
    CTerrainObject( CScene* pScene );
    virtual ~CTerrainObject();
	virtual int32 GetRenderElementHandles( CRenderElement** vRenderElementHandle, int32 nMaxCount );
    bool32 Save( CFileWriter* pFileWrite );
    bool32 Open( const char* strResourceSpace, CXML* pXML, HXMLNODEHANDLE hSceneHandle );
    void SetSize( const char* strResourceSpace, int32 nBlockCountX, int32 nBlockCountY, float fBlockSize );
    void GetBlockDim( int32 *pBlockSizeX, int32 *pBlockSizeY );
    CTerrainBlock* GetBlock( int32 nCoordX, int32 nCoordY);
    CTerrainBlock* SelectBlock( CRaycaster* pRaycaster, Vector3 out_ );
    bool32 GetCellPos( float x, float y, int32 *pCellX_, int32* pCellY_ );
    float GetCellHeight( int32 nCellX, int32 nCellY );
    void SetCellHeight( int32 nCellX, int32 nCellY, float fHeight );
    color32 GetCellColor( int32 nCellX, int32 nCellY );
    void SetCellColor( int32 nCellX, int32 nCellY, color32 cHeight );
    int32 GetCellBlock( int32 nCellX, int32 nCellY, CTerrainBlock* vTerrainBlock[4] );
    float GetBlockGeometryHeight( float x, float y );
    bool32 SetLayerTexture( int32 nCellX, int32 nCellY, const char* strTextureFile, int32 nBrushType, int32 nBrushSize, int32 nBrushAlpha );

public:
    int32 m_nBlockCountX, m_nBlockCountY;
	float m_fBlockSize;

    float m_fStartPosX, m_fStartPosY;
    int32 m_nCellCountX, m_nCellCountY;
    float m_fCellSize;
    float m_fHalfCellSize;

    uint16 m_vTerrainBlockIndexData[512];
    int32 m_nTerrainBlockIndexCount;
    Ref<CIndexBuffer*> m_rTerrainBlockIndexBuffer;

    CScene* m_pScene;
    Ref< CTerrainTextureList* > m_rTerrainTextureList;
    Ref< CRenderItem* >m_rRenderItem;
    CTerrainBlock*** m_ppvTerrainBlockList;
	RefLinkList<CTerrainBlock*> m_vTerrainBlockPool;

    float *m_pHeightMap;
    color32 *m_pColorMap;

};


#endif

